<?php

namespace App\Http\Controllers\Api;

use App\Events\addLog;
use App\Http\Controllers\Controller;
use App\Http\Resources\StudentResource;
use App\Models\Entity;
use App\Models\PoliceStation;
use App\Models\Student;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AddStudentController extends Controller
{


    public function store(Request $request)
    {
        \DB::beginTransaction();
        try {
            $father_name = $request->get('father_name');
            $father_works = $request->get('father_works');
            $father_id_code = $request->get('father_id_code');
            $father_phone_number = $request->get('father_phone_number');
            $mother_name = $request->get('mother_name');
            $mother_id_code = $request->get('mother_id_code');
            $mother_works = $request->get('mother_works');
            $mother_phone_number = $request->get('mother_phone_number');
            $twins = $request->get('twins');
            $family_address = $request->get('family_address');
            $father_type = $request->get('father_type');
            $mother_type = $request->get('mother_type');
            $children = $request->get('children');
            $is_twins = $request->get('is_twins');
            if($is_twins == true){
                $is_twins = 1;
            }else{
                $is_twins = 0;
            }

            $model = new Entity();
            $model->father_name = $father_name;
            $model->father_works = $father_works;
            $model->father_id_code = $father_id_code;
            $model->father_phone_number = $father_phone_number;
            $model->mother_name = $mother_name;
            $model->mother_id_code = $mother_id_code;
            $model->mother_works = $mother_works;
            $model->mother_phone_number = $mother_phone_number;
            $model->family_address = $family_address;
            $model->father_type = $father_type;
            $model->mother_type = $mother_type;

            $model->save();
            $firstStudent = new Student();
            $item = $children[0];
            $exist = Student::query()->where('id_code',$item['id_code'])->first();
            if($exist){
                throw new \Exception($item['name'].'身份证号已存在！');
            }
            $firstStudent->name = $item['name'];
            $firstStudent->school_id = $item['school_id'];
            $firstStudent->sex = $item['sex'];
            $firstStudent->nation = $item['nation'];
            $firstStudent->police_station_id = $item['police_station_id'];
            $firstStudent->id_code = $item['id_code'];
            $firstStudent->is_twins = $is_twins;
            $firstStudent->first_kid = 1;

            $firstStudent->entity_id = $model->id;
            $firstStudent->relation = $item['relation'];
            $firstStudent->prophylactic_code = $item['prophylactic_code'];
            $police_station_id = $item['police_station_id'];
            $firstStudent->twins = $twins;
            $firstStudent->section_id = PoliceStation::query()->where('id',$police_station_id)->first()->section_id;
            $firstStudent->save();
            event(new addLog(Auth::user(),'录入'.$firstStudent->name.'的报名信息'));
            foreach ($children  as $index => $item){
                if ($index == 0){
                    continue;
                }
                $student = new Student();
                $student->name = $item['name'];
                $student->id_code = $item['id_code'];
                $student->nation = $item['nation'];
                $student->sex = $item['sex'];
                $student->prophylactic_code = $item['prophylactic_code'];
                $student->is_twins = $firstStudent->is_twins;
                $student->entity_id = $model->id;
                $student->school_id = $children[0]['school_id'];
                $student->section_id = $firstStudent->section_id;
                $student->relation = $firstStudent->relation ;
                $student->first_kid = 0;
                $student->twins = $twins;
                $student->police_station_id = $firstStudent->police_station_id ;
                $is_exist = Student::query()->where('id_code',$item['id_code'])->first();
                if($is_exist){
                    throw new \Exception($item['name'].'的身份证号已存在！');
                }
                $student->save();
                event(new addLog(Auth::user(),'录入'.$item['name'].'的报名信息'));
            }
            \DB::commit();
            return new StudentResource($firstStudent);
        } catch (\Exception $e) {
            \DB::rollBack();
            return response()->json(['message'=>$e->getMessage()], 481);
        }
    }
}
